package com.university.dialog;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.json.JSONArray;
import org.json.JSONException;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.myuniversity.MainActivity;
import com.university.utils.Flag;
import com.university.utils.SqliteDatabaseManager;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class GetDataDialog {
	private Context mContext;
	private ProgressDialog dialog;
	private SqliteDatabaseManager manager;
	private RequestQueue mQueue;
	public GetDataDialog(Context context) {
		mContext=context;
		dialog=new ProgressDialog(mContext, ProgressDialog.STYLE_SPINNER);
		dialog.setTitle("提示");
		dialog.setMessage("正在更新数据，请稍等...");
		dialog.setCancelable(false);
		dialog.show();
		manager=SqliteDatabaseManager.getInstance(mContext);
	    //网络volley框架相关
        mQueue=Volley.newRequestQueue(mContext.getApplicationContext());
        StringRequest request=new StringRequest(Flag.UniversityUrl, new Response.Listener<String>() {
		@Override
		public void onResponse(String response) {
			String str="";
			try {
				str=new String(response.getBytes("ISO-8859-1"),"utf-8");
				response=null;
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			//异步任务用于将数据存入数据库，因为数据量较大，所以会阻塞主界面，需要也不处理
			new AsyncTask<String, Void, Void>(){
				@Override
				protected Void doInBackground(String... s) {
					JSONArray array=null;
					try {
						array = new JSONArray(s[0]);
						s[0]=null;
					} catch (JSONException e1) {
						e1.printStackTrace();
					}
						ArrayList<ContentValues> arrayList=new ArrayList<ContentValues>();
						JSONArray a=null;
						ContentValues values=null;
						for (int i=0;i<array.length();i++) {
							try {
								a=array.getJSONArray(i);
								values=new ContentValues();
								try {
									values.put("院校代码",a.getString(0));
									values.put("院校名称", a.getString(1));
									values.put("批次", a.getString(2));
									values.put("地区", a.getString(3));
									values.put("文理", a.getString(4));
									values.put("选修", a.getString(5));
									values.put("最低分", a.getInt(6));
									values.put("最高分", a.getInt(7));
									values.put("录取数", a.getInt(8));
								} catch (JSONException e) {
									e.printStackTrace();
								}
								arrayList.add(values);
								if ((i%500==0&&i!=0)||i==(array.length()-1)) {
									manager.insertUniversityData(arrayList);
									//数据插入完成后，清空列表。防止内存泄露
									arrayList.clear();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
					} 
					return null;
				}
				@Override
				protected void onPostExecute(Void result) {
					dialog.dismiss();
					Toast.makeText(mContext, "数据更新成功", 0).show();
				}
			}.execute(str);
		}
		}, new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				dialog.dismiss();
				Toast.makeText(mContext, "数据更新失败", 0).show();
			}
		});
        mQueue.add(request);
		}
		public void disMiss(){
			dialog.dismiss();
		}
		public void show(){
			dialog.show();
		}
}
